package com.cityfood.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cityfood.common.pojo.CityFoodResult;
import com.cityfood.common.pojo.ProductSpeceDisplay;
import com.cityfood.common.utils.JsonUtils;
import com.cityfood.pojo.CfProduct;
import com.cityfood.pojo.CfProductSpecs;
import com.cityfood.service.ProductService;
import com.cityfood.service.ProductSpecsService;
import com.github.pagehelper.PageInfo;

@Controller
public class ProductSpecsController {
	
	//商品规格接口
	@Autowired
	private ProductSpecsService specsService;
	
	//商品接口
	@Autowired
	private ProductService productService;
	
	
	
	//后台管理---------------------------------------------------------------------------

	//根据商品id查找商品信息
	/**
	 * 
	 * @param productId   默认传0 全选   不为0 按商品ID过滤
	 * @param currentPage
	 * @return
	 */
	@RequestMapping(value="/manage/specs",method=RequestMethod.GET)
	@ResponseBody
	public CityFoodResult getProductSpecs(@RequestParam(defaultValue="0") Integer productId,
										  @RequestParam(defaultValue="1")Integer currentPage){
		
		CityFoodResult cityFoodResult = new CityFoodResult();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		
		List<Map<String,Object>> ls = new ArrayList<Map<String,Object>>();
		//根据商品id查询商品的所有规格
		PageInfo<CfProductSpecs> specses = specsService.getSpecsByProductIdAndPage(productId, currentPage);
		if (specses==null) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("查询商品规格失败");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
		if (specses.getList().size()==0) {
			cityFoodResult.setCode(201);
			cityFoodResult.setMsg("该商品暂无规格");
			cityFoodResult.setData("{}");
			return cityFoodResult;
		}
		for (CfProductSpecs specs : specses.getList()) {
			Map<String,Object> ms = new LinkedHashMap<String,Object>();
			CfProduct product = productService.getProductByProductId(specs.getProductId());
			ms.put("speceId", specs.getId());
			ms.put("productName", product.getName());
			ms.put("prodcutId", specs.getProductId());
			ms.put("specsPic", specs.getSpecsImage());
			ms.put("price", specs.getPrice().doubleValue());
			Map taste = JsonUtils.jsonToMap(specs.getProductSpecs());
			ms.put("productSpecs", taste);
			ms.put("stock", specs.getStock());
			ms.put("createTime", sd.format(specs.getCreateTime()));
			ls.add(ms);
		}
		Map<String,Object> mp = new LinkedHashMap<>();
		//存入查询到的总数
		mp.put("total", specses.getTotal());
		//存入需要展示的数据
		mp.put("specss", ls);
		cityFoodResult.setCode(200);
		cityFoodResult.setMsg("查询商品规格成功");
		cityFoodResult.setData(mp);
		return cityFoodResult;	
		
	}
	
	
		//根据商品规格信息添加商品规格，insert
		@RequestMapping(value="/manage/specs",method=RequestMethod.POST)
		@ResponseBody
		public CityFoodResult insertProductSpecs(@RequestBody Map<String,Object> map){
			CityFoodResult cityFoodResult = new CityFoodResult();
			
			Integer productId = Integer.parseInt(map.get("productId").toString());
			String specs = (String)map.get("productSpecs");
			String specsImage = "";
			if (map.containsKey("specsImage")) {
				specsImage = (String)map.get("specsImage");
			}
			Integer stock = Integer.parseInt(map.get("stock").toString());
			BigDecimal price = new BigDecimal(map.get("price").toString());
			
			CfProductSpecs productSpecs = new CfProductSpecs();
			productSpecs.setProductId(productId);
			productSpecs.setProductSpecs(specs);
			productSpecs.setSpecsImage(specsImage);
			productSpecs.setStock(stock);
			productSpecs.setPrice(price);
			productSpecs.setSalesVolume(0);
			productSpecs.setIsdisplay((byte)1);
			productSpecs.setCreateTime(new Date());
			productSpecs.setUpdateTime(new Date());
			
			int i = specsService.insertProductSpecs(productSpecs);
			if (i>0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("添加商品规格成功");
				cityFoodResult.setData(true);
			}else{
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("添加商品规格失败");
				cityFoodResult.setData(false);
			}
			return cityFoodResult;
		}

		//根据商品规格信息修改商品规格,update
		@RequestMapping(value="/manage/specs",method=RequestMethod.PUT)
		@ResponseBody
		public CityFoodResult updateProductSpecs(@RequestBody Map<String,Object> map){
			CityFoodResult cityFoodResult = new CityFoodResult();
			
			Integer id = Integer.parseInt(map.get("id").toString());
			Integer productId = Integer.parseInt(map.get("productId").toString());
			String specs = (String)map.get("productSpecs");
			String specsImage = "";
			if (map.containsKey("specsImage")) {
				specsImage = (String)map.get("specsImage");
			}
			Integer stock = Integer.parseInt(map.get("stock").toString());
			BigDecimal price = new BigDecimal(map.get("price").toString());
			Byte isDisplay = 1;
			if (map.containsKey("isDisplay")) {
				isDisplay = Byte.parseByte(map.get("isDisplay").toString());
			}
			
			
			CfProductSpecs productSpecs = new CfProductSpecs();
			productSpecs.setId(id);
			productSpecs.setProductId(productId);
			productSpecs.setProductSpecs(specs);
			productSpecs.setSpecsImage(specsImage);
			productSpecs.setStock(stock);
			productSpecs.setPrice(price);
			productSpecs.setIsdisplay(isDisplay);
			productSpecs.setUpdateTime(new Date());
			
			int i = specsService.updateProductSpecs(productSpecs);
			if (i>0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("修改商品规格成功");
				cityFoodResult.setData(true);
			}else{
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("修改商品规格失败");
				cityFoodResult.setData(false);
			}
			return cityFoodResult;
		}
		
		//根据商品规格信息删除商品规格,delete
		@RequestMapping(value="/manage/specs/remove",method=RequestMethod.DELETE)
		@ResponseBody
		public CityFoodResult deleteProductSpecs(@RequestBody Map map){
		    String id = map.get("id").toString();
			CityFoodResult cityFoodResult = new CityFoodResult();
			CfProductSpecs productSpecs=new CfProductSpecs();
			productSpecs.setId(Integer.parseInt(id));
			int i = specsService.deleteProductSpecs(productSpecs);
			if (i>0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("删除商品规格成功");
				cityFoodResult.setData(true);
			}else{
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("删除商品规格失败");
				cityFoodResult.setData(false);
			}
			return cityFoodResult;
		}
		
	//根据商品规格信息删除商品规格,delete
	@RequestMapping(value = "/manage/specs/batchremove", method = RequestMethod.DELETE)
	@ResponseBody
	public CityFoodResult deleteProductSpecses(@RequestBody Map map){
	    String ids = map.get("ids").toString();
		CityFoodResult cityFoodResult = new CityFoodResult();
		String[] strIds = ids.split(",");
		for (String strId : strIds) {
			CfProductSpecs productSpecs = new CfProductSpecs();
			productSpecs.setId(Integer.parseInt(strId));

			int i = specsService.deleteProductSpecs(productSpecs);
			if (i > 0) {
				cityFoodResult.setCode(200);
				cityFoodResult.setMsg("删除商品规格成功");
				cityFoodResult.setData(true);
			} else {
				cityFoodResult.setCode(201);
				cityFoodResult.setMsg("删除商品规格失败");
				cityFoodResult.setData(false);
				return cityFoodResult;
			}
		}

		return cityFoodResult;
	}				

}
